package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequiredLogin;
import cn.wolfcode.wolf2w.annotation.UserInfoParam;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.query.TravelCommentQuery;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.redis.service.IStatisticsVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StatisticsVO;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/travels")
public class TravelController {

    @Autowired
    private ITravelService travelService;
    @Autowired
    private ITravelCommentService travelCommentService;
    @Autowired
    private IStatisticsVORedisService statisticsVORedisService;
    @GetMapping("/query")
    public Object query(TravelQuery qo, @UserInfoParam UserInfo user){
        return JsonResult.success(travelService.queryForApi(qo, user));
    }

    @GetMapping("/detail")
    public Object detail(String id, @UserInfoParam UserInfo user){
        if (StringUtils.hasLength(id)) {
            return JsonResult.success(travelService.get(id));
        }
        return JsonResult.success();
    }

     @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(Travel travel, @UserInfoParam UserInfo user){
        // 设置用户的id进去
         if (user == null){
             throw new LogicException("请先登录");
         }
         travelService.saveOrUpdate(travel, user);
        return JsonResult.success(travel.getId());
    }

    @RequiredLogin
    @PostMapping("/commentAdd")
    public Object commentAdd(TravelComment travelComment, @UserInfoParam UserInfo user){
        BeanUtils.copyProperties(user, travelComment);
        travelComment.setUserId(user.getId());
        travelCommentService.save(travelComment);
        return JsonResult.success();

    }

    @GetMapping("/comments")
    public Object comments(String travelId){
        return JsonResult.success(travelCommentService.queryByTravelId(travelId));
    }

    @PostMapping("/travelThumbUp")
    @RequiredLogin
    public Object travelThumbUp(String tid, @UserInfoParam UserInfo userInfo){
        boolean ret = statisticsVORedisService.thumbUp(tid, userInfo.getId());
        return JsonResult.success(ret);
    }

    @GetMapping("/statisVo")
    public Object statisVo(String tid){
        StatisticsVO vo = statisticsVORedisService.getStatisticsVOByTravel(tid);
        return JsonResult.success(vo);
    }



    //上传图片
    @RequestMapping("/contentImage")
    public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
        UMEditorUploader up = new UMEditorUploader(request);
        String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
        up.setAllowFiles(fileType);
        up.setMaxSize(10000); //单位KB
        up.upload(upfile);

        String callback = request.getParameter("callback");
        String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
                +", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
        result = result.replaceAll( "\\\\", "\\\\" );
        if(callback == null ){
            return result ;
        }else{
            return "<script>"+ callback +"(" + result + ")</script>";
        }
    }


}
